<!--
 * @Author: XieQiu 雪球
 * @Date: 2021-05-10 15:06:59
 * @LastEditTime: 2022-03-01 14:52:30
 * @LastEditors: Please set LastEditors
 * @Description: 历史巡查记录页面
-->
<template>
  <BaseMainSlot :show-btn-box="false">
    <template v-slot:search-box>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName==='1'">收起筛选</p>
              <p v-if="activeName===''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="search()">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">巡查任务</div>
            <el-input v-model="pointName" placeholder="请输入巡查任务名称" clearable @input="search" />
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">巡查路线</div>
            <el-select filterable clearable v-model="routeId" clearable filterable>
              <el-option
                v-for="item in patrolLine"
                :key="item.routeId"
                :label="item.routeName"
                :value="item.routeId"
              />
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">巡查人</div>
            <el-select filterable clearable v-model="staffId" clearable filterable>
              <el-option
                v-for="item in staffData"
                :key="item.staffId"
                :label="item.staffName"
                :value="item.staffId"
              />
            </el-select>
          </div>
          <div class="select-box-content select-box-leng-content">
            <div class="select-box-content-name">巡查时间</div>
            <StartTimeEndTimeSelectionBox @startVal="startVal" @endVal="endVal" />
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:table-box>
      <el-table
        ref="tableData"
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="88%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="taskName" label="巡查任务" />
        <el-table-column prop="routeName" label="巡查线路" />
        <el-table-column prop="staffName" label="巡查人" />
        <el-table-column prop="generateTime" label="巡查时间" min-width="100">
          <template v-if="scope.row.taskId" slot-scope="scope">
            {{ scope.row.generateTime ? dateToFormat(scope.row.generateTime) : "无"
            }}
            ~ {{ scope.row.endTime ? dateToFormat(scope.row.endTime) : "无" }}
          </template>
        </el-table-column>
        <el-table-column label="巡查结果">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.taskId"
              type="text"
              size="mini"
              @click="showResult(scope.row.taskId)"
            >查看巡查结果</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <!-- 查询结果数据 -->
      <el-dialog class="dialog" title="查询结果" :visible.sync="isShowResult" width="60%" center>
        <div class="result">
          <el-table v-loading="resultLoading" highlight-current-row :data="resultData" border>
            <el-table-column prop="pointName" label="巡查点" />
            <el-table-column prop="result" label="巡查结果">
              <template slot-scope="scope">
                <span v-if="scope.row.taskResultStatus === 'NORMAL'">正常</span>
                <span v-if="scope.row.taskResultStatus === 'ABNORMAL'">异常</span>
              </template>
            </el-table-column>
            <el-table-column prop="resultDescribe" label="结果描述" />
            <el-table-column label="查看图片">
              <template slot-scope="scope">
                <el-button
                  v-if="scope.row.pictureId"
                  type="text"
                  size="mini"
                  @click="showResultmessage(scope.row.pictureId)"
                >查看</el-button>
                <el-button v-if="!scope.row.pictureId" type="text" style="color:#a49090" disabled>查看</el-button>
              </template>
            </el-table-column>
            <!-- <el-table-column label="查看异常图片">
              <template slot-scope="scope">
                <el-button
                  v-if="scope.row.taskResultStatus === 'ABNORMAL'"
                  type="text"
                  size="mini"
                  @click="showResultmessage(scope.row.pictureId)"
                >查看</el-button>
                <el-button
                  v-if="scope.row.taskResultStatus === 'NORMAL'"
                  type="text"
                  disabled
                  style="color:#a49090"
                >查看</el-button>
              </template>
            </el-table-column>-->
          </el-table>
        </div>
      </el-dialog>

      <el-dialog class="dialog dialogIMG" :visible.sync="isShowimage" width="60%" center>
        <img
          v-for="item in imageUrlList"
          :key="item.pictureId"
          class="imgmessage"
          width="100%"
          :src="item.pictureData"
        />
        <!-- </div> -->
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import Pagination from "@/components/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import SearchBox from "@/components/SearchBox";
import { blobToBase64 } from "@/utils/convert";
import StartTimeEndTimeSelectionBox from "@/components/StartTimeEndTimeSelectionBox";
import historyPatrolApi from "@/api/pond-patrol-management/history-patrol-record";
import staffApi from "@/api/basic-info-manage/staff/index";
import patrolInfoApi from "@/api/pond-patrol-management/patrol-line-manage";
import { mymixin } from "@/utils/mixins";
export default {
  name: "HistoryPatrolRecord",
  components: {
    Pagination,
    BaseMainSlot,
    StartTimeEndTimeSelectionBox,
    SearchBox
  },
  mixins: [mymixin],
  data() {
    return {
      // 选中的数据项数组 ID组成
      taskIdList: [],
      // 是否显示巡查结果
      isShowResult: false,
      // 显示鱼塘批次图片详细信息
      isShowResultmessage: false,
      // 展示图片
      isShowimage: false,
      // 鱼塘批次详细信息列表
      resultDatatwo: [],
      // 图片地址
      nowimageurl: "",
      resultLoading: true,
      resultData: [],
      // 所有巡查人列表
      allStaffList: [],
      // 所有巡查路线列表
      allLineList: [],
      // search值
      // 时间间隔
      rangeTime: "",
      // 巡查路线ID
      routeId: "",
      pointName: "",
      // 巡查人ID
      staffId: "",
      staffData: [],
      dataTimeStart: "",
      inspectionTaskName: "",
      dataTimeEnd: "",
      // 图片
      imageUrlList: [],
      startTime: "",
      endTime: "",
      patrolLine: []
    };
  },
  watch: {},
  created() {
    this.queryDataList();
  },
  mounted() {
    // 获取表格数据
    this.search();
  },
  methods: {
    getTableDataList() {
      this.tableData = [];
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          // organizationNo: sessionStorage.organizationNo,
          inspectionRouteId: this.inspectionRouteId || undefined,
          staffName: this.staffName || undefined,
          inspectionTaskName: this.inspectionTaskName || undefined,
          dataTimeStart: this.startTime || undefined,
          dataTimeEnd: this.endTime || undefined,
          beforeFlagList: [2, 3],
          checkFlag: true,
          organizationNo: sessionStorage.organizationNo
        }
      };
      historyPatrolApi.queryPageByCondition(params).then(res => {
        this.loading = false;
        let data = res.data;
        // 赋值总条数
        if (data) {
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // 巡查人
    async queryDataList() {
      const { data: staff } = await staffApi.queryByCondition({});
      if (staff !== undefined) {
        this.staffData = staff;
      } else this.staffData = [];
      const { data: resp } = await patrolInfoApi.queryByCondition();
      this.patrolLine = resp;
    },
    // 时间转换
    dateFormat(row, colnum) {
      if (row.generateTime) {
        return this.$moment(row[colnum.property]).format("YYYY-MM-DD HH:mm:ss");
      }
      if (row.endTime) {
        return this.$moment(row[colnum.property]).format("YYYY-MM-DD HH:mm:ss");
      }
    },
    // 搜索框时间
    startVal(e) {
      this.startTime = e;
    },
    endVal(e) {
      this.endTime = e;
    },
    searchs() {
      // let time1 = this.$moment(this.rangeTime[0]).format("YYYY-MM-DD HH:mm:ss");
      this.currentPage = 1;
      this.showResult();
    },
    handleEdit(row) {
      // console.log(row);
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify(row));
      // this.$refs.formData.resetFields();
      // console.log(this.formData)
    },
    dateToFormat(originDate) {
      const dt = new Date(originDate);
      const y = dt.getFullYear();
      const m = (dt.getMonth() + 1 + "").padStart(2, "0");
      const d = (dt.getDate() + "").padStart(2, "0");

      const hh = (dt.getHours() + "").padStart(2, "0");
      const mm = (dt.getMinutes() + "").padStart(2, "0");
      const ss = (dt.getSeconds() + "").padStart(2, "0");

      return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    },
    handleSelectionChange(list) {
      this.taskIdList = [];
      this.taskIdList = list.map(item => item.taskId);
      if (this.taskIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    // 查看图片
    showResultmessage(Id) {
      let params = {
        pictureId: Id
      };
      historyPatrolApi.queryByConditionList(params).then(async res => {
        this.imageUrlList = [{ pictureData: await blobToBase64(res) }];
        if (this.imageUrlList.length > 0) this.isShowimage = true;
        else this.$message.warning("暂无图片");
      });
    },
    // 获取结果
    showResult(taskId) {
      if (taskId) {
        this.isShowResult = true;
        if (taskId) {
          let params = {
            taskId: taskId
          };
          historyPatrolApi.getInspectionResultList(params).then(res => {
            this.resultLoading = false;
            this.resultData = res.data;
            // console.log(this.resultData);
          });
        }
      } else {
        return;
      }
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
.common-layout {
  border: 1px solid $borderColor;
  height: calc(100% - #{$actionBarHeight + 10});
  padding: 10px;
  .search-box {
    display: flex;
    align-items: center;
    .label-input {
      display: flex;
      align-items: center;
      .label {
        min-width: 80px !important;
        white-space: nowrap;
      }
      // .label-80 {
      //   // width: 80px;
      // }
    }
    .search {
      display: flex;
      align-items: center;
      img {
        height: 30px;
      }
    }
  }
  .content {
    height: calc(100% - 50px);
    margin: 20px 0;
    .table-box {
      position: relative;
      height: 100%;
      overflow-y: auto;
      .el-table {
        width: 100%;
      }
    }
  }
}
// }
::v-deep .el-dialog {
  overflow-y: auto;
  // background-color: rgba($color: #82ceee, $alpha: 0.8);
  .el-form {
    display: flex;
    .el-form-item {
      display: flex;
    }
  }
  .searchbox {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .label-input {
      color: cornsilk;
      display: flex;
      align-items: center;
      .label {
        margin-right: 2px;
      }
    }
  }
  .el-table__empty-text {
    color: #000 !important;
  }
  .el-dialog__body {
    text-align: initial;
    padding: 20px 10px 10px !important;
  }
}

::v-deep .el-dialog {
  border: 1px solid #000;
  border-radius: 10px;
  .el-dialog__header {
    border-radius: 9px 9px 0 0;
    .el-dialog__title {
      color: #fff;
    }
  }
  .el-dialog__body {
    padding: 5px 20px;
    font-size: 12px !important;
    .el-input {
      input {
        font-size: 12px;
      }
    }
  }
  .el-table {
    max-height: 200px;
    overflow-y: auto;
    background-color: rgba(255, 255, 255, 1);
    .cell {
      color: #000;
    }
    th {
      background-color: rgba(230, 230, 230, 1);
    }
    th.is-leaf {
      border-color: rgba(77, 77, 77, 1);
    }
    td {
      border-color: rgba(77, 77, 77, 1);
    }
    tr:hover {
      background-color: rgba(77, 77, 77, 1);
    }
    .el-table__empty-text {
      color: #000;
    }
    .current-row {
      // background-color: rgb(177, 177, 177);
      background-color: #dbdbdb !important;
    }
    .el-table__row--striped {
      background-color: rgba(255, 255, 255, 1);
    }
    // color: #000;
  }
  .el-table--border {
    border-color: rgba(77, 77, 77, 1);
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
  }
  .el-table--enable-row-hover .el-table__body tr:hover > td {
    background-color: rgb(177, 177, 177);
  }
  .el-table__body tr.current-row > td {
    background-color: #dbdbdb !important;
  }
  .el-form-item {
    margin-bottom: 10px;
  }
  .total-price {
    color: #000;
  }
  .el-table__body {
    // height: ;
    height: 0;
    // background-color: #003947;
  }
}
::v-deep .dialogIMG {
  .el-dialog {
    height: 80%;
    .el-dialog__header {
      padding: 30px;
    }
    .el-dialog__body {
      height: 92%;
      overflow-y: auto !important;
    }
  }
}
</style>
